package com.intelligent.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.intelligent.entity.Outbound;
import com.intelligent.entity.OutboundDetail;
import com.intelligent.entity.Product;
import com.intelligent.entity.Unit;
import com.intelligent.entity.Warehouse;
import com.intelligent.entity.TUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;

/**
 * MyBatis Plus 配置类
 * 解决lambda cache问题
 */
@Configuration
@Slf4j
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
        return interceptor;
    }

    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        // 启用缓存，这有助于解决lambda cache问题
        globalConfig.setDbConfig(dbConfig);
        return globalConfig;
    }

    /**
     * 初始化时强制预加载实体类，解决lambda cache问题
     */
    @PostConstruct
    public void initLambdaCache() {
        try {
            // 强制加载实体类，确保lambda缓存正确初始化
            Class.forName("com.intelligent.entity.Outbound");
            Class.forName("com.intelligent.entity.OutboundDetail");
            Class.forName("com.intelligent.entity.Product");
            Class.forName("com.intelligent.entity.Unit");
            Class.forName("com.intelligent.entity.Warehouse");
            Class.forName("com.intelligent.entity.TUser");

            // 初始化lambda缓存
            initAllLambdaCache();

            log.info("Lambda cache entities preloaded successfully in workbench service");
        } catch (Exception e) {
            log.error("Failed to preload entity classes in workbench service: {}", e.getMessage(), e);
        }
    }

    /**
     * 初始化所有相关实体类的lambda缓存
     * 通过创建LambdaQueryWrapper来触发缓存初始化
     */
    private void initAllLambdaCache() {
        try {
            // 初始化Outbound的lambda缓存
            LambdaQueryWrapper<Outbound> outboundWrapper = new LambdaQueryWrapper<>();
            outboundWrapper.eq(Outbound::getOutboundSerialNumber, "dummy");
            outboundWrapper.orderByDesc(Outbound::getOutboundTime);
            
            // 初始化OutboundDetail的lambda缓存
            LambdaQueryWrapper<OutboundDetail> detailWrapper = new LambdaQueryWrapper<>();
            detailWrapper.eq(OutboundDetail::getOutboundNo, "dummy");
            detailWrapper.orderByDesc(OutboundDetail::getId);
            
            // 初始化Product的lambda缓存
            LambdaQueryWrapper<Product> productWrapper = new LambdaQueryWrapper<>();
            productWrapper.like(Product::getProductName, "dummy");
            productWrapper.eq(Product::getCategory, 1);
            
            // 初始化其他相关实体的lambda缓存
            LambdaQueryWrapper<Unit> unitWrapper = new LambdaQueryWrapper<>();
            unitWrapper.eq(Unit::getUnitName, "dummy");
            
            LambdaQueryWrapper<Warehouse> warehouseWrapper = new LambdaQueryWrapper<>();
            warehouseWrapper.eq(Warehouse::getWarehouseName, "dummy");
            
            LambdaQueryWrapper<TUser> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(TUser::getUsername, "dummy");
            
            log.info("Lambda cache initialized successfully for all entities in workbench service");
        } catch (Exception e) {
            log.error("Failed to initialize lambda cache in workbench service: {}", e.getMessage(), e);
            throw new RuntimeException("Lambda cache initialization failed in workbench service", e);
        }
    }
}
